package com.zero.manage.web.config;


import com.zero.manage.web.utils.FileUtil;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;

/**
 * @author zero
 * 添加虚拟映射路径  以   FileUtil.RELATIVE_PATH+ "**" 访问的静态资源 都会去  "file:"+ FileConfigBean.getUploadPath() 寻找
 * 若部署在linux 中 需注意更改 FileConfigBean.getUploadPath() 的路径  linux 上可没有 CDEFG 盘符  exmaple: file:/home/app/
 */
@Configuration
public class WebMvcConfigurerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(FileUtil.RELATIVE_PATH + "**").addResourceLocations("file:" + FileUtil.RELATIVE_PATH);
    }

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //单个文件最大MB
        factory.setMaxFileSize(DataSize.ofMegabytes(10L));
        //设置总上传文件总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(100L));
        return factory.createMultipartConfig();
    }
}